package com.graduation.fq.student.Process;

import com.alibaba.fastjson.JSONArray;
import com.graduation.fq.student.Utils.Const;
import com.graduation.fq.student.domain.CourseVO;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by jifang
 * Time 2016/5/27 15:09.
 */
public class CourseCheckProcess extends BaseProcess {

    private boolean isAll;
    private int credit;

    private List<CourseVO> courses;

    public CourseCheckProcess(int credit) {
        this.isAll = false;
        this.credit = credit;
    }

    public CourseCheckProcess(){
        isAll = true;
    }

    public boolean isAll() {
        return isAll;
    }

    public void setAll(boolean all) {
        isAll = all;
    }

    public int getCredit() {
        return credit;
    }

    public void setCredit(int credit) {
        this.credit = credit;
    }

    public List<CourseVO> getCourses() {
        return courses;
    }

    public void setCourses(List<CourseVO> courses) {
        this.courses = courses;
    }

    @Override
    String getRequestUrl() {
        if (isAll()) {
            return "/course/get_all_course.do";
        } else {
            return String.format("/course/get_course_by_credit.do?credit=%s", getCredit());
        }
    }

    @Override
    String getParameter() {
        return null;
    }

    @Override
    void onResult(JSONObject object) {
        int status = object.optInt("status");
        setStatus(status);
        if (status == Const.HTTP_SUCCESS) {
            String json = object.optJSONArray("data").toString();
            List<CourseVO> courses = JSONArray.parseArray(json, CourseVO.class);
            setCourses(courses);
        }
    }
}
